package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.TeacherReportDetailItemBean;
import com.fancy.learncenter.bean.TeacherReportDetailListBean;
import com.fancy.learncenter.bean.UrgeDataBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.TeacherReportDetailAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 教师端报告详情
 */
public class CartoonTeacherReportDetailActivity extends BaseActivity {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.right_img)
    ImageView rightImg;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.package_name_tv)
    TextView packageNameTv;
    @Bind(R.id.complete)
    TextView complete;
    @Bind(R.id.total)
    TextView total;
    @Bind(R.id.package_name_layout)
    LinearLayout packageNameLayout;
    @Bind(R.id.lesson_name_text)
    TextView lessonNameText;
    @Bind(R.id.all_urge)
    TextView allUrge;
    @Bind(R.id.unit_name_text)
    TextView unitNameText;
    @Bind(R.id.time_text)
    TextView timeText;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<TeacherReportDetailItemBean> list;
    private TeacherReportDetailAdapter adapter;
    private String homePackageId;
    private String weekNum;
    private String classId;
    private String className;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_report_detail, Status.STATUS);
        ButterKnife.bind(this);
        getIntentData();
        initView();
        initData();
    }



    String totalNum = "";
    String commitNum = "";

    String unitName = "";
    String packageName = "";
    String lessonName = "";
    String timeName = "";

    private void initView() {
        initToolbar("报告详情");
        list = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new TeacherReportDetailAdapter(this, list);
        adapter.setHomePackageId(homePackageId);
        adapter.setUrgeCallBackl(new TeacherReportDetailAdapter.UrgeCallBack() {
            @Override
            public void urgeCallBackClick(String studentHomeworkId, int position) {
                urgeStudent(studentHomeworkId);

                adapter.itemDatas.get(position).setIsNotify("1");
                adapter.notifyItemChanged(position);
            }

        });
        recyclerView.setAdapter(adapter);


        packageNameTv.setText(packageName);
        unitNameText.setText(unitName);
        lessonNameText.setText(lessonName);

        total.setText("/" + totalNum);

        complete.setText(commitNum);
        timeText.setText(timeName);
    }
    private void getIntentData() {
        homePackageId = getIntent().getStringExtra(JumpIntentKey.HOMEWORK_PACKAGE_ID);
        weekNum = getIntent().getStringExtra(JumpIntentKey.WEEKNUM);
        classId = getIntent().getStringExtra(JumpIntentKey.CLASSID);

        unitName = getIntent().getStringExtra(JumpIntentKey.UNIT_NAME);

        packageName = getIntent().getStringExtra(JumpIntentKey.HOME_PACKAGE_NAME);

        lessonName = getIntent().getStringExtra(JumpIntentKey.LESSON_NAME);

        classId = getIntent().getStringExtra(JumpIntentKey.CLASSID);

        totalNum = getIntent().getStringExtra(JumpIntentKey.TOTAL_NUM);

        commitNum = getIntent().getStringExtra(JumpIntentKey.COMMIT_NUM);

        timeName = getIntent().getStringExtra(JumpIntentKey.TIME_NUM);

        className = getIntent().getStringExtra(JumpIntentKey.CLASSNAME);
    }

    String studentHomeworkId;

    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("homeworkPackageId", homePackageId);
        map.put("weekNum", weekNum);
        map.put("classId", classId);
        HttpMehtod.getInstance().cartoonListStudentHomework(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<TeacherReportDetailListBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), TeacherReportDetailListBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<TeacherReportDetailListBean>() {
                    @Override
                    public void success(TeacherReportDetailListBean dataBean) {

                        if (dataBean != null) {
                            list.addAll(dataBean.getResult());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }


    @OnClick(R.id.all_urge)
    public void onViewClicked() {

        Map<String, Object> map = new HashMap<>();
        map.put("homeworkPackageId", homePackageId);
        map.put("weekNum", weekNum);
        map.put("homeworkName", packageName);
        map.put("classId", classId);
        map.put("homeworkEndTime", timeName);
        map.put("msgType", "1");

        HttpMehtod.getInstance().urgeStudentHomework(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<UrgeDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), UrgeDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<UrgeDataBean>() {
                    @Override
                    public void success(UrgeDataBean dataBean) {
                        if (dataBean.isIsSuccess()) {
                            ToastUtil.show("催促成功");
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }

    private void urgeStudent(String studentHomeworkId) {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);

        map.put("homeworkPackageId", homePackageId);

        map.put("weekNum", weekNum);

        map.put("homeworkName", packageName);

        map.put("homeworkEndTime", timeName);

        map.put("msgType", "1");


        map.put("className:", className);

        map.put("studentHomeworkId", studentHomeworkId);

        HttpMehtod.getInstance().urgeStudentHomework(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<UrgeDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), UrgeDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<UrgeDataBean>() {
                    @Override
                    public void success(UrgeDataBean dataBean) {
                        if (dataBean.isIsSuccess()) {
                            ToastUtil.show("催促成功");
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }
}
